# -*- coding: utf-8 -*-

import globalvar
import datetime

class   emsdatabase():

    
    def __init__(self):
        pass
    def getfloat(self,instr):

        if(instr !=""):
            outstr = float(instr)
        else:
            outstr = 0
        
        return outstr        
        
    def getcustomercode(self,code):
        if(code == ""):
            n=datetime.datetime.now()
            id_str = n.isoformat(' ')  
            id_str=id_str[-20:-1]                          
        else:
            id_str=code
        return id_str        
    def checksmsnotify(self,sms,sphone):
        if(sms == 1 and sphone == "" ):
            sms = 0
        return sms    
                
    def uploadreceivebill(self,all_values_list):


        globalvar.socketlogging.debug("upload receive bill")

        customer_code = all_values_list[0]
        customer_code = self.getcustomercode(customer_code)

        bill_count = all_values_list[1]
        bill_count = int(bill_count,16)    

        scan_type = all_values_list[2]
        
        serial_number = all_values_list[3]
        sender_name = all_values_list[4]

        globalvar.socketlogging.debug("customer_code %s"%(customer_code))
        globalvar.socketlogging.debug("bill_count %s"%(bill_count))        
        globalvar.socketlogging.debug("scan_type %s"%(scan_type))
        globalvar.socketlogging.debug("serial_number %s"%(serial_number))

        sender_name = sender_name.decode('utf-8').encode('gbk')        

        
        for index in range(0,bill_count):
            
            bill_code = all_values_list[5 + 7 * index ]
            payment_type = all_values_list[5 + 7 * index + 1]
            payment_type = int(payment_type,16)
            
            tran_cost = all_values_list[5 + 7 * index + 2]
            tran_cost = self.getfloat(tran_cost)
            cash_on_delivery =all_values_list[5 + 7 * index + 3]
            cash_on_delivery = self.getfloat(cash_on_delivery)
            insured_amount = all_values_list[5 + 7 * index + 4]
            insured_amount = self.getfloat(insured_amount)
            sms_notify = int(all_values_list[5 + 7 * index + 5],16)
            sender_phone = all_values_list[5 + 7 * index + 6]                    

            globalvar.socketlogging.debug("bill_code %s"%(bill_code))
            globalvar.socketlogging.debug("payment_type %s"%(payment_type))
            globalvar.socketlogging.debug("tran_cost %s"%(tran_cost))
            globalvar.socketlogging.debug("cash_on_delivery %s"%(cash_on_delivery))        
            globalvar.socketlogging.debug("insured_amount %s"%(insured_amount))
            globalvar.socketlogging.debug("sms_notify %s"%(sms_notify))
            globalvar.socketlogging.debug("sender_phone %s"%(sender_phone))

            sms_notify = self.checksmsnotify(sms_notify, sender_phone)
            
            receiver_phone = '0'
            n=datetime.datetime.now()
            id_str = n.isoformat(' ')            
            insert_record_str = "insert into TAB_GPRS_SCAN_QY\
             (%s) values\
                ('%s','%s', '321001'  , '456001'    , '001'        ,\
                 '%s'     , ''        , 'scan_man',  'scan_site', sysdate,\
        'dispatchorman', '%s'         , ''        , '22'        , '0'        ,\
                '01'   , '0'       , 'ww001'   , 'q001'      , 'test001',\
                '25'   , '10.00'   , 'signname', 1           , '%s',\
                 '%s' ,%d,\
                 '%s',%d,%f,%f,%f,'%s')"%(globalvar.TAB_GPRS_SCAN_QY_COLUMN,id_str,bill_code,scan_type,sender_phone,sender_phone,receiver_phone,sms_notify,
                                             customer_code, payment_type,tran_cost,cash_on_delivery,insured_amount,serial_number)                 
            globalvar.socketlogging.debug("insert_record_str %s"%(insert_record_str))                 
            globalvar.cursor.execute(insert_record_str)
            globalvar.conn.commit()        
            
        update_str = "UPDATE TAB_GPRS_MSG SET MSG_STATE=2 ,bill_code = '%s' WHERE SERIAL_NUMBER='%s' AND MSG_STATE=1;"%(bill_code,serial_number)
        globalvar.databaselogging.debug("upate_str = %s"%(update_str))
        globalvar.cursor.execute(update_str)
        globalvar.conn.commit()

            
         
            
    def newreceivebill(self,all_values_list):

        globalvar.socketlogging.debug("new receive bill")
        
        customer_code = all_values_list[0]
        customer_code = self.getcustomercode(customer_code)

        bill_count = all_values_list[1]
        bill_count = int(bill_count,16)    

        scan_type = all_values_list[2]

        globalvar.socketlogging.debug("customer_code %s"%(customer_code))
        globalvar.socketlogging.debug("bill_count %s"%(bill_count))        
        globalvar.socketlogging.debug("scan_type %s"%(scan_type))
        
        for index in range(0,bill_count):
            
            bill_code = all_values_list[3 + 7 * index ]
            payment_type = all_values_list[3 + 7 * index + 1]
            payment_type = int(payment_type,16)
            
            tran_cost = all_values_list[3 + 7 * index + 2]
            tran_cost = self.getfloat(tran_cost)
            cash_on_delivery =all_values_list[3 + 7 * index + 3]
            cash_on_delivery = self.getfloat(cash_on_delivery)
            insured_amount = all_values_list[3 + 7 * index + 4]
            insured_amount = self.getfloat(insured_amount)
            sms_notify = int(all_values_list[3 + 7 * index + 5],16)
            sender_phone = all_values_list[3 + 7 * index + 6]                    

            globalvar.socketlogging.debug("bill_code %s"%(bill_code))
            globalvar.socketlogging.debug("payment_type %s"%(payment_type))
            globalvar.socketlogging.debug("tran_cost %s"%(tran_cost))
            globalvar.socketlogging.debug("cash_on_delivery %s"%(cash_on_delivery))        
            globalvar.socketlogging.debug("insured_amount %s"%(insured_amount))
            globalvar.socketlogging.debug("sms_notify %s"%(sms_notify))
            globalvar.socketlogging.debug("sender_phone %s"%(sender_phone))

            sms_notify = self.checksmsnotify(sms_notify, sender_phone)
            
            receiver_phone = '0'
            n=datetime.datetime.now()
            id_str = n.isoformat(' ')            
            insert_record_str = "insert into TAB_GPRS_SCAN_QY\
             (%s) values\
                ('%s','%s', '321001'  , '456001'    , '001'        ,\
                 '%s'     , ''        , 'scan_man',  'scan_site', sysdate,\
        'dispatchorman', 'sendername'         , ''        , '22'        , '0'        ,\
                '01'   , '0'       , 'ww001'   , 'q001'      , 'test001',\
                '25'   , '10.00'   , 'signname', 1           , '%s',\
                 '%s' ,%d,\
                 '%s',%d,%f,%f,%f,'')"%(globalvar.TAB_GPRS_SCAN_QY_COLUMN,id_str,bill_code,scan_type,sender_phone,receiver_phone,sms_notify,
                                             customer_code, payment_type,tran_cost,cash_on_delivery,insured_amount)                 
            globalvar.socketlogging.debug("insert_record_str %s"%(insert_record_str))                 
            globalvar.cursor.execute(insert_record_str)
            globalvar.conn.commit()
                     
            
    def updatereceivebill(self,all_values_list):
        bill_code = all_values_list[0]
        signin_name = all_values_list[1]

        
        
    
